package com.coldchain.admin.user.api;

import com.coldchain.admin.user.entity.Role;
import com.coldchain.admin.user.service.IRoleService;
import com.coldchain.authclient.annotation.CheckClientToken;
import com.coldchain.authclient.annotation.CheckUserToken;
import com.coldchain.common.msg.BaseResponse;
import com.coldchain.common.msg.ObjectRestResponse;
import com.fangliai.core.context.BaseContextHandler;
import com.fangliai.core.entitiy.SystemModelEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author fangliai
 * @since 2018-09-11
 */
@RestController
@RequestMapping("/role")
@CheckClientToken
@CheckUserToken
@Api(value = "角色模块")
public class RoleController {
    private static final Logger log= LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private IRoleService roleService;

    @GetMapping("/list")
    @ApiOperation(value = "获取所有的角色列表")
    public ObjectRestResponse<Role> list(){
         return new ObjectRestResponse<>().data(roleService.findRoleByList());
    }



    @PostMapping("/edit")
    @ApiOperation(value ="角色编辑")
    @ApiImplicitParam(value = "id",name = "id",dataType = "string",paramType = "query",required = true)
    public BaseResponse edit(Role role){
        try {
            if(StringUtils.isEmpty(role.getId())){
                switch (SystemModelEnum.valueOf(BaseContextHandler.getSysModl())){
                    case CYS:
                        role.setTargetId(BaseContextHandler.getCysID());
                        break;
                    case USER:
                        role.setTargetId(BaseContextHandler.getCommpanyId());
                        break;
                }
                role.setSysmodel(BaseContextHandler.getSysModl());
                role.setTargetId(BaseContextHandler.getCysIdOrCommpanyId());
                role.setCreateBy(BaseContextHandler.getUsername());
                role.setCreateTime(new Date());
            }else {
                role.setModBy(BaseContextHandler.getUsername());
                role.setModTime(new Date());
            }
            roleService.saveOrUpdate(role);
            return new ObjectRestResponse<>().data("ok");
        }catch (Exception e){
            log.error("编辑角色失败 error{}",e.getMessage());
            return new ObjectRestResponse<>().error("error");
        }
    }

    @GetMapping("/del/{id}")
    @ApiOperation(value ="删除角色")
    @ApiImplicitParam(value = "id",name = "id",dataType = "string",paramType = "path",required = true)
    public BaseResponse del(@PathVariable("id") String id ){
        try {
            roleService.removeById(id);
            return new ObjectRestResponse<>().data("ok");
        }catch (Exception e){
            log.error("删除角色失败 error{}",e.getMessage());
            return new ObjectRestResponse<>().error("error");
        }
    }
    @GetMapping("/info/{id}")
    @ApiOperation(value ="查询角色信息")
    @ApiImplicitParam(value = "id",name = "id",dataType = "string",paramType = "path",required = true)
    public BaseResponse info(@PathVariable("id") String id ){
        try {
            return new ObjectRestResponse<>().data(roleService.getById(id));
        }catch (Exception e){
            log.error("查询角色失败 error{}",e.getMessage());
            return new ObjectRestResponse<>().error("error");
        }
    }

}
